#!/bin/sh

logfile=/home/siebel/scripts/component.log
command=$1
cp=$2
tempfile=command.txt
defaultcomp=SMObjMgr_chs
defaultcmd=restart
srvrcmd="srvrmgr /g siebeltest /s siebeltest /e SBA_81 /u SADMIN /p SADMIN2"
envpath=". /siebel/app/siebel/siebsrvr/siebenv.sh"
configlist="configure list comp show CC_ALIAS,CP_DISP_RUN_STATE,CP_NUM_RUN_TASKS,CP_MAX_TASKS,CP_ACTV_MTS_PROCS,CP_MAX_MTS_PROCS "

if [ "${command}" == "" ]
then
        command=${defaultcmd}
fi

if [ "${cp}" == "" ]
then
        cp=${defaultcomp}
fi

${envpath}

echo -e ${configlist} > $tempfile
echo list comp ${cp} >> $tempfile
preusers=`${srvrcmd} /b /i $tempfile | grep $cp |  grep -v "srvrmgr" | awk '{ print $3 }'`

echo -e "#---------------------- Component Operate ------------------------#" >> ${logfile}
if [ "${command}" == "restart" ]
then

    echo Load Component: ${cp}  >> ${logfile}
    echo Operate Action: ${command}  >> ${logfile}
    echo Kill Component ${cp} ... >> ${logfile}
    ${srvrcmd} /b /c "kill comp ${cp}" | grep ${cp} | grep -v "srvrmgr"
    sleep 2
    echo Start Component ${cp} ... >> ${logfile}
    ${srvrcmd} /b /c "startup comp ${cp}" | grep ${cp} | grep -v "srvrmgr"
else
    echo ${command} Component ${cp} ... >> ${logfile}
    ${srvrcmd} /b /c "${command} comp ${cp}" | grep ${cp} | grep -v "srvrmgr"

    sleep 1
fi

res=`${srvrcmd} /b /i $tempfile | grep $cp |  grep -v "srvrmgr" `

status=`echo $res | awk '{ print $2 }'`

echo Check Componnet Status: ${status} >> ${logfile}

echo Before Operate Running User Number: ${preusers} >> ${logfile}
echo Operate DateTime: $(date +"%Y/%m/%d %T") >> ${logfile}
echo -e "#---------------------------  End  -------------------------------#\n"  >> ${logfile}

rm $tempfile

